//
//  NSTreeNode+WCExtensions.m
//  WabbitCode
//
//  Created by William Towe on 11/9/08.
//  Copyright 2008 Revolution Software. All rights reserved.
//

#import "NSTreeNode+WCExtensions.h"
#import "WCMacros.h"
#import "WCTreeNode.h"


@implementation NSTreeNode (WCExtensions)
- (BOOL)isRoot; {
	return ([self parentNode] == nil)?YES:NO;
}

- (NSArray *)descendantNodes; {
	NSMutableArray *nodes = [NSMutableArray array];
	for (NSTreeNode *node in [self childNodes]) {
		[nodes addObject:node];
		if (![node isLeaf])
			[nodes addObjectsFromArray:[node descendantNodes]];
	}
	return [[nodes copy] autorelease];
}

- (NSArray *)descendantNodesInclusive; {
	return ([self isLeaf])?[NSArray arrayWithObject:self]:[self descendantNodes];
}

- (NSArray *)descendantGroupNodes; {
	NSMutableArray *nodes = [NSMutableArray array];
	for (NSTreeNode *node in [self childNodes]) {
		if (![node isLeaf]) {
			[nodes addObject:node];
			[nodes addObjectsFromArray:[node descendantGroupNodes]];
		}
	}
	return [[nodes copy] autorelease];
}

- (NSArray *)descendantLeafNodes; {
	NSMutableArray *nodes = [NSMutableArray array];
	for (NSTreeNode *node in [self childNodes]) {
		if ([node isLeaf])
			[nodes addObject:node];
		else
			[nodes addObjectsFromArray:[node descendantLeafNodes]];
	}
	return [[nodes copy] autorelease];
}

- (NSArray *)descendantLeafNodesInclusive; {
	return ([self isLeaf])?[NSArray arrayWithObject:self]:[self descendantLeafNodes];
}

- (BOOL)isDescendantOfNode:(NSTreeNode *)node; {
	return [[node descendantNodes] containsObject:self];
}

- (BOOL)isParentOfNode:(NSTreeNode *)node; {
	return [[node parentNode] isEqualTo:self];
}

@end
